package com.example.lock;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

/**
* @ClassName: BubbleLinearLayout
* @Description: 气泡框（默认三角在顶部，改变三角位置请重新处理）
* @Author: Shangqs
* @E-mail:1037105071@qq.com
* @CreateDate: 2021/5/21 9:39
*/
public class BubbleLinearLayout extends LinearLayout {
    private Paint paint;
    private final int DELTA_HEIGHT = 20;
    private final int RADIUS=5;

    public BubbleLinearLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.FILL);
        int paddingBottom = getPaddingBottom();
        int paddingTop = getPaddingTop();
        int paddingLeft = getPaddingLeft();
        int paddingRight = getPaddingRight();
        setPadding(RADIUS+paddingLeft,  DELTA_HEIGHT+RADIUS+paddingTop, RADIUS+paddingRight, RADIUS+paddingBottom);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        int widthSize = getMeasuredWidth();
        int heightSize = getMeasuredHeight();
        paint.setColor(getResources().getColor(R.color.bg_wm_color));
        //画矩形
        RectF rectF = new RectF(0, DELTA_HEIGHT, widthSize, heightSize);
        canvas.drawRoundRect(rectF, RADIUS, RADIUS, paint);
        //画三角形
        Path path = new Path();
        path.moveTo((widthSize / 2) - 25, DELTA_HEIGHT);
        path.lineTo(widthSize / 2, 0);
        path.lineTo((widthSize / 2) + 25, DELTA_HEIGHT);
        path.close();
        canvas.drawPath(path, paint);
        super.dispatchDraw(canvas);
    }


    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
    }
}
